// const { default: api } = require("../../utils/api")
import api from '../../utils/api'
import customerApi from "../../utils/customerApi";

import {
  throttle,
  throttle2,
  withTokenFn
} from '../../utils/util'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resData: {},
    isShow: false,
    type: 1,
    isShowRefund: false,
    isShowVaccine: false,
    isshowAppraise: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, 'options============')
    if (options.type * 1 === 1) {
      this.setData({
        type: options.type * 1,
        orderItemId: options.orderItemId,
      })
    } else {
      this.setData({
        type: options.type * 1,
        id: options.id,
      })
    }
  },
  onShow() {
    if (this.data.type * 1 === 1) {
      // 订单详情
      this.getOrderMsg()
    } else {
      // 疫苗商品详情
      this.getList()
    }
  },
  isShowMore() {
    this.setData({
      isShow: !this.data.isShow
    })
  },

  getList() {
    api.frontVaccineOrderDetailV3({
      data: {
        id: this.data.id
      },
      success: res => {
        console.log(res)
        if (res.code * 1 === 0) {
          this.setData({
            resData: res.data,
            isShowRefund: +res.data.status === 1 || +res.data.status === 3,
            isShowVaccine: +res.data.status === 1,
            isshowAppraise: +res.data.status === 4 && !res.data.hasComment
          })
        }
      }
    })
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: '4001118582'
    })
  },

  copy(e) {
    console.log(e)
    let text = e.currentTarget.dataset.text
    wx.setClipboardData({
      data: text,
      success(res) {
        console.log(res)
      }
    })
  },

  getOrderMsg() {
    api.orderDetailV2({
      data: {
        orderItemId: this.data.orderItemId
      },
      success: res => {
        console.log(res)
        if (res.code * 1 === 0) {
          this.setData({
            resData: res.data,

          })
        }
      }
    })
  },

  moveToVaccine() {
    let {
      source,
      orderNo,
      healthExam
    } = this.data.resData
    if (source && orderNo) {
      throttle(function () {
        wx.navigateTo({
          url: `../registeredVaccine/registeredVaccine?source=${source}&orderNo=${orderNo}&healthExam=${healthExam}`
        })
      }, 2000)
    }

  },

  moveToAppraise() {
    let {
      id
    } = this.data.resData
    app.globalData.userAppraise = this.data.resData
    if (id) {
      throttle(function () {
        wx.navigateTo({
          url: `../userAppraise/userAppraise?vaccineOrderId=${id}`
        })
      }, 2000)
    }
  },

  jumpConsultByThrottle2: throttle2(function () {
    this.getRefund();
  }, 2000),

  getRefund() {
    withTokenFn(() => {
      customerApi.refund({
        data: {},
        success: (res) => {
          if (res.code * 1 === 0) {
            app.globalData.customerChoose = res.data.conversation;
            wx.navigateTo({
              url: `../customerIntelligent/index`,
            });
          } else {
            wx.showToast({
              title: '请联系在线客服做相关退款',
              icon: 'none',
              duration: 1500,
            });
          }
        },
        fail: () => {
          wx.showToast({
            title: '请联系在线客服做相关退款',
            icon: 'none',
            duration: 1500,
          });
        }
      });
    });
  },

  moveToRefund() {
    this.jumpConsultByThrottle2()
  },
})